package com.fishery.customization.dao;

import com.fishery.customization.dao.reposity.BaseDao;
import com.fishery.customization.entity.UserTheme;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;

@Repository
public class UserThemeDao extends BaseDao<UserTheme> {

	public String findThemeIdOfUserByUserId(String userId) {
		Map<String, Object> fields = new HashMap<>(2);
		fields.put("_id", 0);
		fields.put("theme_id", 1);
		return mongoOperations.findOne(getBasicQuery("user_id", userId, fields),
				UserTheme.class).getThemeId();
	}

	public long upsertThemeOfUser(String userId, String themeId) {
		return mongoOperations.upsert(Query.query(Criteria.where("user_id").is(userId))
				, Update.update("theme_id", themeId), getCollectionName()).wasAcknowledged() ? 1 : 0;
	}
}
